package org.javaymw.springboot.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.apache.commons.lang3.StringUtils;
import org.javaymw.springboot.dto.MemberDto;
import org.javaymw.springboot.entity.MemberEntity;
import org.javaymw.springboot.mapper.MemberMapper;
import org.javaymw.springboot.service.MemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/7/12 12:44
 * @Description:
 */
@Service
@Transactional
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberEntity> implements MemberService {

    private static final Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public MemberDto getMemberByParam(String name, String passWord) {
        MemberEntity entity = new MemberEntity();
        entity.setName(name);
        entity.setPassWord(passWord);
        MemberEntity memberEntity = memberMapper.selectOne(entity);
        if (memberEntity == null) {
            return null;
        }
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(memberEntity, memberDto);
        return memberDto;
    }

    @Override
    public Integer getMemberCountByName(String name) {
        Wrapper<MemberEntity> wrapper = new EntityWrapper<MemberEntity>();
        wrapper.eq("name", name);
        return memberMapper.selectCount(wrapper);
    }

    @Override
    public void saveMember(MemberDto bizDto) {
        MemberEntity bizEntity = new MemberEntity();
        BeanUtils.copyProperties(bizDto, bizEntity);
        memberMapper.insert(bizEntity);
    }

    @Override
    public Page<MemberDto> getMemberByPage(int pageNum, int pageSize, String searchParam, String startDate, String endDate) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String, Object> columnMap = new HashMap<String, Object>(2);
        if (searchParam != null && !StringUtils.isEmpty(searchParam)) {
            columnMap.put("name", searchParam);
        }
        if (!StringUtils.isBlank(startDate)) {
            columnMap.put("startDate", startDate);
        }
        if (!StringUtils.isBlank(endDate)) {
            columnMap.put("endDate", endDate);
        }
        Page<MemberEntity> memberEntities = memberMapper.findMemberByPage(columnMap);
        if (memberEntities == null || memberEntities.isEmpty()) {
            return null;
        }

        Page<MemberDto> memberDtos = new Page<MemberDto>();
        for (MemberEntity memberEntity : memberEntities) {
            MemberDto memberDto = new MemberDto();
            BeanUtils.copyProperties(memberEntity, memberDto);
            memberDtos.add(memberDto);
        }

        return toDto(memberEntities, memberDtos);
    }

    @Override
    public Integer getMemberCountByEmail(String email) {
        Wrapper<MemberEntity> wrapper = new EntityWrapper<MemberEntity>();
        wrapper.eq("email", email);
        return memberMapper.selectCount(wrapper);
    }

    @Override
    public MemberDto getMemberByEmail(String email) {
        Wrapper<MemberEntity> wrapper = new EntityWrapper<MemberEntity>();
        wrapper.eq("email", email);
        MemberEntity memberEntity = memberMapper.selectList(wrapper).get(0);
        if (memberEntity == null) {
            return null;
        }
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(memberEntity, memberDto);
        return memberDto;
    }

    @Override
    public MemberDto getMemberById(Long id) {
        MemberEntity memberEntity = memberMapper.selectById(id);
        if (memberEntity == null) {
            return null;
        }
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(memberEntity, memberDto);
        return memberDto;
    }

    @Override
    public Integer updateMemberById(MemberDto memberDto) {
        MemberEntity memberEntity = new MemberEntity();
        BeanUtils.copyProperties(memberDto, memberEntity);
        return memberMapper.updateById(memberEntity);
    }

    @Override
    public Integer update(MemberDto memberDto) {
        MemberEntity memberEntity = new MemberEntity();
        BeanUtils.copyProperties(memberDto, memberEntity);
        return memberMapper.updateById(memberEntity);
    }

    @Override
    public Integer getMemberCountByPhone(String phone) {
        Wrapper<MemberEntity> wrapper = new EntityWrapper<MemberEntity>();
        wrapper.eq("phone", phone);
        return memberMapper.selectCount(wrapper);
    }

    private Page<MemberDto> toDto(Page<MemberEntity> memberEntities, Page<MemberDto> memberDtos) {
        BeanUtils.copyProperties(memberEntities, memberDtos);
        return memberDtos;
    }
}
